/**************************************************************************
 *         Copyright(c) 2008 by Sunplus mMedia  Technology Co., Ltd.      *
 *                                                                        *
 *  This software is copyrighted by and is the property of Sunplus mMedia *
 *  Technology Co., Ltd. All rights are reserved by Sunplus mMedia Techno-*
 *  logy Co., Ltd. This software may only be used in accordance with the  *
 *  corresponding license agreement. Any unauthorized use, duplication,   *
 *  distribution, or disclosure of this software is expressly forbidden.  *
 *                                                                        *
 *  This Copyright notice "M U S T" not be removed or modified without    *
 *  prior written consent of Sunplus mMedia Technology  Co., Ltd.         *
 *                                                                        *
 *  Sunplus mMedia Technology Co., Ltd. reserves the right to modify this *
 *  software without notice.                                              *
 *                                                                        *
 *  Sunplus mMedia Technology Co., Ltd.                                   *
 *  19-1, Innovation First Road, Science-Based Industrial Park,           *
 *  Hsin-Chu, Taiwan, R.O.C.                                              *
 *                                                                        *
 *  Author: Ted Chiu                                                      *
 **************************************************************************/
#include <stdio.h>
#include "general.h"
#include "disp_api.h"
#include "panel.h"

#define	DEBUG	0
//TPG85 v1.2
//TPG105 v1.3
#define	SUPPORT_051	0

void TD015THEB2_Flip(UINT8 mode);
void TD015THEB2_PowerOff(void);
void TD015THEB2_PowerOn(void);
void DELAY_1ms(void);
/* TPG85 or TPG105 */
/* R02, bit 1~0
      	Data Format 	Operating frequency
-----  --------------------------------------- -------------------
 00 	RGBDummy		24.54 MHz
 01 	YUV				24.54 MHz
 10 	Through modet	10.36 MHz
*/
#define	TD015THEB2_UPS052	0
#define	TD015THEB2_YUV		1
#define	TD015THEB2_UPS051	2

/* Direction Control
   ============================
	R05[0]  0 		1(Default)
			H_FLIP	Normal
	R05[1] 	0		1(Default)
			V_FLIP	Normal
*/
extern UINT8 SpiInitMode;	/* bit[0]:CLK, bit[1]:DAT bit[2]:CS*/
extern tagPanelOff  pPanelOff;
extern tagPanelOn   pPanelOn;
extern tagPanelFlip pPanelFlip;

#if	SUPPORT_051
static	UINT32	color[][4] = {
	{ 0x06, 0x0F, 0x05, 0x0B },	/* TD015THEB2_UPS051 */
};
#endif
UINT8 TD015THEB2_mode;
void TD015THEB2_Init(UINT8 mode)
{
	UINT8 sel, i;
	dispPreInit();
	TD015THEB2_mode = mode;
	//printf("TD015THEB2_Init\n");
	SpiInitMode = 0x04;	//CS HI, DAT/CLK LO
	XBYTE[0x2D00] = mode & 0x0F;
	XBYTE[0X2D06] = 1;	//vsync width
	XBYTE[0X2D07] = 1;	//hsync width
	XBYTE[0X2D27] = 8;	//ups_ck_phase
	switch (mode)
	{
		#if	SUPPORT_051
		case UPS051:	/* 480*240, 10.67MHz */
			dispPnlVhSet(659, 262+0x20, 93, 13, 480, 240);
	    	dispClkDivSet(96,9);
			XBYTE[0X2D26] = 0x29;	//TFT-LCD RGB cycle defin
			sel = TD015THEB2_UPS051;
			break;
		#endif

		case UPS052:	/* 320*240, 24MHz */
			dispPnlVhSet(390, 264+0x20, 56, 20, 320, 240);
	    	dispClkDivSet(96,4);
			XBYTE[0x2D26] = 0;	//TFT-LCD RGB cycle defin
			XBYTE[0x2D28] = 1;
			sel = TD015THEB2_UPS052;
			break;

		default:
			break;
	}

#if	DEBUG
printf("%s %u\n", __FILE__, (unsigned short)__LINE__);
for(i=0; i<200; i++)
	DELAY_1ms();
#endif

	XBYTE[0x2D77] = 0xFF;
	XBYTE[0x2D78] = 0x0D;

#if	DEBUG
printf("%s %u\n", __FILE__, (unsigned short)__LINE__);
for(i=0; i<200; i++)
	dispVsyncWait(DISP_SIGNAL_RISING);
#endif

	dispEn(1);	//Step2: Before turn on VCC, the VD/HD/DCLK input signal must keep still until Tpwr(2ms).
	dispRegUpd();

#if	DEBUG
printf("%s %u\n", __FILE__, (unsigned short)__LINE__);
for(i=0; i<200; i++)
	dispVsyncWait(DISP_SIGNAL_RISING);
#endif

	for(i=0; i<10; i++)
		dispVsyncWait(DISP_SIGNAL_RISING);

	dispSpiCtrl((0x02<<10)+sel, 16, 0);
	dispSpiCtrl((0x03<<10)+0x01, 16, 0);
	if(mode & 0x80)
		dispSpiCtrl((0x04<<10)+0x0F, 16, 0);	//power management

#if	DEBUG
printf("%s %u\n", __FILE__, (unsigned short)__LINE__);
for(i=0; i<200; i++)
	dispVsyncWait(DISP_SIGNAL_RISING);
#endif

	dispSpiCtrl((0x05<<10)+0x2B, 16, 0);
	dispSpiCtrl((0x07<<10)+0x08, 16, 0);

#if	DEBUG
printf("%s %u\n", __FILE__, (unsigned short)__LINE__);
for(i=0; i<200; i++)
	dispVsyncWait(DISP_SIGNAL_RISING);
#endif

	XBYTE[0x2D77] = 0x00;
	XBYTE[0x2D78] = 0x00;

#if	DEBUG
printf("%s %u\n", __FILE__, (unsigned short)__LINE__);
for(i=0; i<200; i++)
	dispVsyncWait(DISP_SIGNAL_RISING);
#endif

	pPanelOff = TD015THEB2_PowerOff;
	pPanelOn  = TD015THEB2_PowerOn;
	pPanelFlip= TD015THEB2_Flip;
	return;
}

void TD015THEB2_PowerOff(void)
{
	UINT8 i;
	//dispSpiCtrl((0x04<<10)+0x0E, 16, 0);	//power management
	dispSpiCtrl((0x04<<10)+0x0A, 16, 0);	//power management
	for(i=0; i<11; i++) {
		dispVsyncWait(DISP_SIGNAL_RISING);
	}
	//printf("TD015THEB2_PowerOff\n");
}

void TD015THEB2_PowerOn(void)
{
	dispSpiCtrl((0x04<<10)+0x0F, 16, 0);	//power management
}

void TD015THEB2_Flip(UINT8 mode)
{
	UINT8 reg=(5<<10)+0x2B;
	if(mode > 3)	mode = 0;
	if(mode & DISP_FLIP_H)	{	/* Left/Right Flipped */
		reg &= ~0x0001;
	}
	else	{
	}

	if(mode & DISP_FLIP_V)	{	/* Up/Down Flipped */
		reg &= ~0x0002;
	}

	switch (XBYTE[0x2D00] & 0x0F)
	{
		#if	SUPPORT_051
		case UPS051:	/* 640*240, 12.95MHz */
			dispColorSet(color[0][mode]);
			break;
		#endif

		case UPS052:	/* 320*240, 24MHz */
			break;
	}
	dispSpiCtrl((0x02<<10)+0, 16, 0);
	dispSpiCtrl((0x03<<10)+0x01, 16, 0);
	dispSpiCtrl((0x04<<10)+0x0F, 16, 0);	//power management
	dispSpiCtrl(reg, 16, 0);
	dispSpiCtrl((0x07<<10)+0x08, 16, 0);
	dispRegUpd();
}
